<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/19
 * Time: 下午3:46
 */

namespace app\admin\controller;


use app\src\account\enmu\AccountEnmu;
use app\src\accountScore\logic\AccountScoreLogic;
use app\src\filiation\action\FiliationAction;
use app\src\platform\emnu\PlatformType;
use app\src\platform\logic\PlatformAdminLogic;
use app\src\platform\logic\PlatformLogic;
use app\src\platformApply\logic\PlatformApplyLogic;
use app\src\powersystem\enum\AdminRoleEnum;
use app\src\powersystem\logic\AuthGroupAccessLogic;
use app\src\system\logic\CityLogic;
use app\src\system\logic\ProvinceLogic;
use app\src\user\logic\VUserInfoLogic;

class Platform extends Admin
{
    /**
     * 获取下级平台
     */
    public function queryChild()
    {
        if(IS_AJAX){
            $platform_id = $this->_param('platform_id');
            if(empty($platform_id)) $this->error('平台id缺失');
            $PlatformLogic = new PlatformLogic;
            $result = $PlatformLogic->childAll($platform_id);
            if($result['status']){
                //查询父级平台
                $list = [];
                foreach ($result['info'] as $platform){
                    if($platform['parent_id'] == 0 && $platform['group_id'] = PlatformType::HEADQUARTERS){
                        $platform['parent_info'] = null;
                    }else{
                        $result = $PlatformLogic->getPlatformInfo($platform['parent_id']);
                        if(!$result['status']) $this->error('获取下级平台失败', null, $result['info']);
                        $platform['parent_info'] = $result['info'];
                        if($platform['group_id'] == 3) $type = 4;
                        if($platform['group_id'] == 2) $type = 3;
                        if($platform['group_id'] == 1) $type = 1;
                        $result = (new FiliationAction())->memberAndMoney($platform['id'],$type);
                       // $pscore = (new AccountScoreLogic)->getScore(AccountEnmu::PLATFORM, $platform['id']);
                        $platform['score'] = $result['info']['account_sum'];
                        $platform['member_count'] = $result['info']['member_count'];
                    }
                    $list[] = $platform;
                }
                $this->success('获取下级平台成功', null, $list);
            }else{
                $this->error('获取下级平台失败', null, $result['info']);
            }
        }
    }

    /**
     * 获取管理员所有平台
     */
    public function getAdminPlatform()
    {
        if(IS_AJAX){
            $result = (new PlatformAdminLogic)->getAdminPlatform(UID);
            if($result['status']){
                //管理员与后台操作员可以管理总部
                if(empty($result['info']) && $this->isBackAdmin()){
                    $result['info'] = [
                        [
                            'platform_id' => 1,
                            'group_id' => PlatformType::HEADQUARTERS,
                            'name' => '总部',
                            'id' => 1
                        ]
                    ];
                }
                $this->success('获取管理所有平台成功', null, $result['info']);
            }else{
                $this->error('获取管理所有平台失败', null, $result['info']);
            }

        }
    }

    /**
     * 获取平台信息
     */
    public function getInfo()
    {
        if(IS_AJAX){
            $platform_id = $this->_param('platform_id');
            if(empty($platform_id)) $this->error('平台id缺失');
            $result = (new PlatformLogic)->getPlatformInfo($platform_id);
            if($result['status']){
                $this->success('获取平台详情成功', null, $result['info']);
            }else{
                $this->error('获取平台详情失败', null, $result['info']);
            }
        }
    }

    /**
     * 获取平台管理员
     */
    public function getPlatformAdmin()
    {
        if(IS_AJAX){
            $platform_id = $this->_param('platform_id');
            if(empty($platform_id)) $this->error('平台id缺失');
            $result = (new PlatformLogic)->queryAllAdmin($platform_id);
            if($result['status']){
                $this->success('获取平台管理员成功', null, $result['info']);
            }else{
                $this->error('获取平台管理员失败', null, $result['info']);
            }
        }
    }

    public function delete()
    {
        if(IS_AJAX){
            $platform_id = $this->_param('platform_id');

        }
    }

    /**
     * 移除指定平台指定管理员
     */
    public function removeAdmin()
    {
        if(IS_AJAX){
            $platform_id = $this->_param('platform_id');
            $uid = $this->_param('uid');
            if(empty($platform_id)) $this->error('平台id缺失');
            if(empty($uid)) $this->error('管理员id缺失');
            $result = (new PlatformLogic)->removePlatformAdmin($platform_id, $uid);
            if($result['status']){
                $this->success('移除平台管理员成功', null, $result['info']);
            }else{
                $this->error('移除平台管理员失败', null, $result['info']);
            }
        }
    }

    /**
     * 添加平台管理员
     */
    public function addAdmin()
    {
        if(IS_AJAX){
            $platform_id = $this->_param('platform_id');
            $mobile = $this->_param('mobile');
            if(empty($platform_id)) $this->error('平台id缺失');
            if(empty($mobile)) $this->error('手机账号缺失');
            //查询该手机用户id
            $result = (new VUserInfoLogic)->getInfo(['mobile' => $mobile]);
            if(!$result['status'])  $this->error('添加平台管理员失败', null, $result['info']);
            if(empty($result['info'])) $this->error('不存在的用户', null);
            $uid = $result['info']['id'];
            $result = (new PlatformLogic)->setPlatformAdmin($platform_id, $uid);
            if($result['status']){
                $this->success('添加平台管理员成功', null, $result['info']);
            }else{
                $this->error('添加平台管理员失败', null, $result['info']);
            }
        }
    }

    /**
     * 编辑平台名称
     */
    public function editName()
    {
        if(IS_AJAX){
            $platform_id = $this->_param('platform_id');
            $name = $this->_param('name');
            if(empty($platform_id)) $this->error('平台id缺失');
            if(empty($name)) $this->error('名字不能为空');
            //查询名称是否重复
            $result = (new PlatformLogic)->getInfo(['name' => $name]);
            if(!empty($result['info'])&&$result['status']) $this->error('平台名称重复', null);

            $result = (new PlatformLogic)->saveByID($platform_id, ['name'=>$name]);
            if($result['status']){
                $this->success('平台名称修改成功', null, $result['info']);
            }else{
                $this->error('平台名称修改失败', null, $result['info']);
            }
        }
    }
    /**
     * 添加平台
     */
    public function addPlatform()
    {
        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $group_id = $this->_param('group_id');
            $parent_id = $this->_param('parent_id');
            $name = $this->_param('name');
            $city_name = $this->_param('city_name');
            $province_name = $this->_param('province_name');

            if(empty($group_id)) $this->error('group_id缺失');
            if(empty($parent_id)) $this->error('parent_id 缺失');
            if(empty($name)) $this->error('name 缺失');

            //查询city_code, province_code
            $result = (new CityLogic)->getInfo(['city' => ['like',"%$city_name%"]]);
            if(!$result['status']) $this->error('添加平台失败', null, $result['info']);
            if($group_id == PlatformType::CITY_SERVICE){
                if(empty($result['info'])) $this->error('错误的城市名称', null);
            }
            $city_code = $result['info']['cityID'];

            $result = (new ProvinceLogic)->getInfo(['province' => ['like',"%$province_name%"]]);
            if(!$result['status']) $this->error('添加平台失败', null, $result['info']);
            if($group_id == PlatformType::PROVINCE_SERVICE){
                if(empty($result['info'])) $this->error('错误的省名称', null);
            }
            $province_code = $result['info']['provinceID'];

            $entity = [
                'group_id' => $group_id,
                'name' => $name,
                'city_code' => $city_code,
                'province_code' => $province_code,
                'preferential_money' => 0,
            ];

            $result = (new PlatformLogic)->addPlatform($parent_id, $entity);
            if($result['status']){
                $this->success('添加平台成功', null, $result['info']);
            }else{
                $this->error('添加平台失败', null, $result['info']);
            }
        }
    }

    /**
     * 删除平台
     */
    public function deletePlatform()
    {
        if(IS_AJAX) {
            if (!$this->isBackAdmin()) {
                $this->error('没有权限查看此内容');
            }

            $platform_id = $this->_param('platform_id');
            if(empty($platform_id)) $this->error('平台id缺失');

            $result = (new PlatformLogic)->deletePlatform($platform_id);

            if($result['status']){
                $this->success('删除平台成功', null, $result['info']);
            }else{
                $this->error('删除平台失败', null, $result['info']);
            }
        }
    }

    /**
     * 平台申请管理
     */
    public function queryApply()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            //查询未处理申请
            $p = $this->_param('p', 1);
            $size = $this->_param('size', 15);
            $page = ['curpage' => $p, 'size' => $size];
            $result = (new PlatformApplyLogic)->applyList($page);
            if($result['status']){
                //上级平台，所属省份，所属城市
                $apply = $result['info'];
                $list = [];
                $PlatformLogic = (new PlatformLogic);
                $ProvinceLogic = (new ProvinceLogic);
                $CityLogic = (new CityLogic);
                foreach ($apply['list'] as $val){
                    //父级名称
                    $result = $PlatformLogic->getPlatformInfo($val['parent_id']);
                    if(!$result['status']) $this->error('获取平台申请列表失败', null, $result['info']);
                    $val['parent_name'] = $result['info']['name'];

                    //省名称
                    $result = $ProvinceLogic->getInfo(['provinceID' => $val['province_code']]);
                    if(!$result['status']) $this->error('获取平台申请列表失败', null, $result['info']);
                    $val['province'] = isset($result['info']['province']) ? $result['info']['province'] : '';

                    //城市名称
                    $result = $CityLogic->getInfo(['cityID' => $val['city_code']]);
                    if(!$result['status']) $this->error('获取平台申请列表失败', null, $result['info']);
                    $val['city'] = isset($result['info']['city']) ? $result['info']['city'] : '';

                    $list[] = $val;

                }
                $apply['list'] = $list;
                $this->success('获取平台申请列表成功', null, $apply);
            }else{
                $this->error('获取平台申请列表失败', null, $result['info']);
            }
        }
    }

    /**
     * 获取平台申请信息
     */
    public function getApplyInfo()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('平台申请id缺失');
            $result = (new PlatformApplyLogic)->applyDetail($apply_id);
            if($result['status']){
                $this->success('获取平台申请信息成功', null, $result['info']);
            }else{
                $this->error('获取平台申请信息失败', null, $result['info']);
            }

        }
    }

    /**
     * 平台申请通过/否决
     */
    public function passApply()
    {
        if(IS_AJAX){
            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $pass = $this->_param('pass');
            $apply_id = $this->_param('apply_id');
            if(empty($apply_id)) $this->error('平台申请id缺失');
            //1通过 2否决
            if(!in_array($pass,[1,2])) $this->error('pass error');
            $pass = $pass == 1;

            $result = (new PlatformApplyLogic)->passApply($apply_id, $pass);
            if($result['status']){
                $this->success('操作成功', null, $result['info']);
            }else{
                $this->error('操作失败', null, $result['info']);
            }
        }
    }

}